package com.senseuni.da.ui.dialog;

import android.content.Context;
import android.net.DhcpInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.text.format.Formatter;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.elvishew.xlog.XLog;
import com.senseuni.common.dialog.CBDialogBuilder;
import com.senseuni.common.dialog.OnDialogResultListener;
import com.senseuni.da.R;
import com.senseuni.da.ui.views.NetworkItemView;
import com.senseuni.da.utils.AppUtils;

public class WifiInfoDialog extends CBDialogBuilder {

    private String ssid;


    public WifiInfoDialog(Context context) {
        super(context, CBDialogBuilder.DIALOG_STYLE_NORMAL, (float) 0.5);
        initView(context);
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_wifi_info, null);

        NetworkItemView viewWifiName = view.findViewById(R.id.view_wifi_name);
        NetworkItemView viewWifiSignal = view.findViewById(R.id.view_wifi_signal);
        NetworkItemView viewWifiStatus = view.findViewById(R.id.view_wifi_status);

        TextView ipView = view.findViewById(R.id.tv_ip);
        TextView dnsView = view.findViewById(R.id.tv_dns);
        TextView advanceView = view.findViewById(R.id.tv_advance);
        TextView macView = view.findViewById(R.id.tv_mac);

        try {
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
            if (wifiInfo != null) {
                ssid =  wifiInfo.getSSID();

                viewWifiName.setTitle(wifiInfo.getSSID());
                viewWifiName.setSummary(R.string.connected);

                viewWifiSignal.setSummary(AppUtils.getSignalDesc(context, wifiInfo.getRssi()));
                viewWifiSignal.setIconResource(AppUtils.getSignRes(context, wifiInfo.getRssi()));

                String ipv4Address = Formatter.formatIpAddress(dhcpInfo.ipAddress);
                ipView.setText(ipv4Address);

                if (dhcpInfo.dns1 != 0) {
                    dnsView.setText(Formatter.formatIpAddress(dhcpInfo.dns1));
                }

//                if (dhcpInfo.gateway != 0) {
//                    viewWifiStatus.setSummary(Formatter.formatIpAddress(dhcpInfo.gateway));
//                }
                String gateway = Formatter.formatIpAddress(dhcpInfo.gateway);

                macView.setText(wifiInfo.getMacAddress());

                int linkSpeed = wifiInfo.getLinkSpeed();
                String supplicantState = wifiInfo.getSupplicantState().toString();

                advanceView.setText("BSSID:" + wifiInfo.getBSSID() +
                        "\nGateway:" + gateway +
                        "\nLink Speed:" + linkSpeed +
                        "\nSupplicant State:" + supplicantState);
            }
        } catch (Exception e) {
            XLog.e("get wifi info error:" + e.getMessage());
        }

        setView(view);
        setTouchOutSideCancelable(true);
        showCancelButton(false);
        showNatureButton(true);
        setNatureButtonText(context.getString(R.string.forget_wifi));
        setButtonClickListener(false, (context1, dialog, whichBtn) -> {
            if (whichBtn == OnDialogBtnClickListener.BUTTON_CONFIRM) {
                dialog.dismiss();
            } else if (whichBtn == OnDialogBtnClickListener.BUTTON_CANCEL) {
                dialog.dismiss();
            } else if (whichBtn == OnDialogBtnClickListener.BUTTON_NATURE) {
                if (mListener != null) {
                    mListener.onResult(dialog, ssid);
                }
            }
        });
    }

    private OnDialogResultListener<String> mListener;

    public void setOnResultListener(OnDialogResultListener<String> listener) {
        mListener = listener;
    }


}
